package com.mm.cloud.common.core.result;

import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * 结果对象
 *
 * @author mr
 * @since 2021-09-15
 */
@Data
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 响应状态编码
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    /**
     * 总条数
     */
    private Long total;

    /**
     * 时间戳
     */
    private Long time = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();

    public Result() {
    }

    /**
     * code、message
     *
     * @param code 响应状态编码
     * @param message 响应信息
     */
    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * @param code 响应状态编码
     * @param message 响应信息
     * @param data 数据
     */
    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * @param code 响应状态编码
     * @param message 响应信息
     * @param data 数据
     * @param total 总条数
     */
    public Result(Integer code, String message, T data, Long total) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.total = total;
    }

    public boolean isSuccess(){
        return this.code == ResultConstants.RESULT_SUCCESS_CODE;
    }

    public boolean isFail(){
        return !isSuccess();
    }
}
